package com.ksr.logistics.controller;

import com.github.pagehelper.PageInfo;
import com.ksr.logistics.pojo.*;
import com.ksr.logistics.service.BaseDataService;
import com.ksr.logistics.service.CustomerService;
import com.ksr.logistics.service.OrderService;
import com.ksr.logistics.service.UserService;
import com.ksr.logistics.utilities.Constant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BaseDataService baseDataService;

    @Autowired
    private UserService userService;

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/list")
    @RequiresPermissions("order:list")
    @ResponseBody
    public PageInfo<Order> show(Integer pageNum, Integer pageSize, String keyword){
        List<Order> datas=orderService.selPageOrders(pageNum,pageSize,keyword);
        PageInfo<Order> infos=new PageInfo<>(datas);
        if (infos.getSize()==0 && infos.getPageNum()>1){
            datas=orderService.selPageOrders(pageNum-1,pageSize,keyword);
            infos=new PageInfo<>(datas);
        }
        return infos;
    }

    @RequestMapping("/detaillist")
    @RequiresPermissions("order:detaillist")
    @ResponseBody
    public List<OrderDetail> showOrderDetail(Long orderId){

        return orderService.selOrderDetailByOrderId(orderId);
    }

    @RequestMapping("/delete")
    @RequiresPermissions("order:delete")
    @ResponseBody
    public Result delete(Long id){
        int res= orderService.deleteById(id);
        Result result=new Result();
        if (1==res){
            result.setCode(1);
            result.setMsg("删除订单成功!");
        }else{
            result.setCode(0);
            result.setMsg("删除订单失败,请重试");
        }
        return result;
    }

    @RequestMapping("/deletes")
    @RequiresPermissions("order:deletes")
    @ResponseBody
    public Result deletes(@RequestParam("ids[]") List<Long> ids){
        int res= orderService.deleteByIds(ids);
        Result result=new Result();
        if (1<=res){
            result.setCode(1);
            result.setMsg("删除订单成功!");
        }else{
            result.setCode(0);
            result.setMsg("删除订单失败,请重试");
        }
        return result;
    }

    @RequestMapping("/edit")
    @RequiresPermissions("order:edit")
    public String orderEdit(Model m,Long orderId){
        if (null!=orderId){
            Order order = orderService.selById(orderId);
            m.addAttribute("order",order);
            List<OrderDetail> orderDetails = orderService.selOrderDetailByOrderId(orderId);
            m.addAttribute("orderDetails",orderDetails);
        }

        List<User> saleMans=userService.selUsersByRoleName(Constant.ROLE_SALESMAN);
        m.addAttribute("saleMans",saleMans);

        List<Customer> customers = customerService.selCustomers();
        m.addAttribute("customers",customers);

        List<BaseData> intervals = baseDataService.selBaseDataByParentName(Constant.BASE_DATA_INTERVAL);
        m.addAttribute("intervals",intervals);

        List<BaseData> payMethods = baseDataService.selBaseDataByParentName(Constant.BASE_DATA_PAYMENT_METHOD);
        m.addAttribute("payMethods",payMethods);

        List<BaseData> freightMethods = baseDataService.selBaseDataByParentName(Constant.BASE_DATA_FREIGHT_METHOD);
        m.addAttribute("freightMethods",freightMethods);

        List<BaseData> takeMethods = baseDataService.selBaseDataByParentName(Constant.BASE_DATA_TAKE_METHOD);
        m.addAttribute("takeMethods",takeMethods);

        return "orderEdit";
    }

    @RequestMapping("/insert")
    @RequiresPermissions("order:insert")
    @ResponseBody
    public Result insert(@RequestBody Order order){
        Result result=new Result(0,"新增订单失败,请重试！");
        int row=orderService.insertOrder(order);
        if (row==1){
            result.setCode(1);
            result.setMsg("新增订单成功");
        }
        return result;
    }

    @RequestMapping("/update")
    @RequiresPermissions("order:update")
    @ResponseBody
    public Result update(@RequestBody Order order){
        Result result=new Result(0,"订单信息修改失败,请重试！");
        int row=orderService.updateOrder(order);
        if (row==1){
            result.setCode(1);
            result.setMsg("订单信息修改成功");
        }
        return result;
    }

    @RequestMapping("/deleteDetail")
    @RequiresPermissions("order:deleteDetail")
    @ResponseBody
    public Result deleteDetail(Long id){
        int res= orderService.deleteDetailById(id);
        Result result=new Result();
        if (1==res){
            result.setCode(1);
            result.setMsg("订单内容删除成功!");
        }else{
            result.setCode(0);
            result.setMsg("订单内容删除失败,请重试");
        }
        return result;
    }

    @RequestMapping("/editDetail")
    @RequiresPermissions("order:editDetail")
    public String orderDetailEdit(Model m,Long orderId,Long detailId,Integer flag){
        m.addAttribute("flag",flag);
        if (detailId!=null){
            OrderDetail orderDetail=orderService.selOrderDetailByDetailId(detailId);
            m.addAttribute("orderDetail",orderDetail);
        }
        if (orderId!=null){
            m.addAttribute("orderId",orderId);
        }
        List<BaseData> units=baseDataService.selBaseDataByParentName(Constant.BASE_DATA_GOODS_UNIT);
        m.addAttribute("units",units);
        return "orderDetailEdit";
    }

    @RequestMapping("/insertDetail")
    @RequiresPermissions("order:insertDetail")
    @ResponseBody
    public Result insertOrderDetail(OrderDetail orderDetail){
        Result result=new Result(0,"新增订单商品失败");
        int row=orderService.insertOrderDetail(orderDetail);
        if (row==1){
            result.setCode(1);
            result.setMsg("新增订单商品成功");
        }

        return result;
    }

    @RequestMapping("/updateDetail")
    @RequiresPermissions("order:updateDetail")
    @ResponseBody
    public Result updateOrderDetail(OrderDetail orderDetail){
        Result result=new Result(0,"修改订单商品内容失败");
        int row=orderService.updateOrderDetail(orderDetail);
        if (row==1){
            result.setCode(1);
            result.setMsg("修改订单商品内容成功");
        }

        return result;
    }

}
